package homework.day02.service;

import homework.day02.mapper.IUserMapper;
import homework.day02.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
public class UserService {
    @Autowired
    private IUserMapper userMapper;

    /**
     * 用户注册功能实现
     */
    public void registerUser() {
        System.out.println("请输入下列注册信息:");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String userName = sc.nextLine().trim();
        if (userMapper.selectByUserName(userName) != null) {
            System.out.println("该用户名已存在，无法再注册！");
            System.exit(0);
        }
        System.out.println("请输入密码:");
        String password = sc.nextLine().trim();
        System.out.println("请输入昵称:");
        String nickName = sc.nextLine().trim();
        System.out.println("请输入年龄:");
        Integer age = Integer.parseInt(sc.nextLine().trim());
        userMapper.insert(new User(null, userName, password, nickName, age));
    }

    /**
     * 用户登录功能实现
     */
    public void loginUser() {
        System.out.println("请输入下列登录信息:");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String userName = sc.nextLine().trim();
        User user = userMapper.selectByUserName(userName);
        if (user == null) {
            System.out.println("用户名错误！");
            System.exit(0);
        }
        System.out.println("请输入密码:");
        String password = sc.nextLine().trim();
        if (user.getPassword().equals(password)){
            System.out.println("信息正确，登录成功！");
        }else {
            System.out.println("密码错误！");
        }
    }

    /**
     * 用户信息修改功能实现
     */
    public void updateUser() {
        System.out.println("请输入用户名:");
        Scanner sc = new Scanner(System.in);
        String userName = sc.nextLine().trim();
        User user = userMapper.selectByUserName(userName);
        if (user == null) {
            System.out.println("用户名不存在！");
            System.exit(0);
        }
        System.out.println("请输入修改的信息:");
        System.out.println("请输入密码:");
        String password = sc.nextLine().trim();
        System.out.println("请输入昵称:");
        String nickName = sc.nextLine().trim();
        System.out.println("请输入年龄:");
        Integer age = Integer.parseInt(sc.nextLine().trim());
        userMapper.update(userName,password,nickName,age);
    }

    /**
     * 用户列表查询
     */
    public void listUser() {
        System.out.println("查询用户列表中....");
        List<User> usersList = userMapper.selectAll();
        for(User user : usersList){
            System.out.println(user);
        }
        System.out.println("查询完毕！！！！");
    }

    /**
     * 删除用户功能实现
     */
    public void deleteUser() {
        System.out.println("进入删除功能....");
        System.out.println("请输入用户名:");
        Scanner sc = new Scanner(System.in);
        String userName = sc.nextLine().trim();
        User user = userMapper.selectByUserName(userName);
        if (user == null) {
            System.out.println("用户名不存在！");
            System.exit(0);
        }
        userMapper.delete(userName);
    }
}
